package com.xhs.hawaii.common.utils;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.dozer.DozerBeanMapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Title.
 * <p>
 * Description.
 *
 * @author tangchuang
 * @version 1.0
 * @since 2017-09-08
 */
public class DozerUtils {


    private static DozerBeanMapper dozer = new DozerBeanMapper();

    private DozerUtils() {

    }

    /**
     * 基于Dozer转换对象的类型.
     */
    public static <T> T transfer(Object source, Class<T> destinationClass) {
        if (source == null) {
            return null;
        }
        return dozer.map(source, destinationClass);
    }

    /**
     * 基于Dozer转换Collection中对象的类型.
     */
    public static <T> List<T> transferList(Collection<?> source, Class<T> destinationClass) {
        if (source == null) {
            return null;
        }
        List<T> destinationList = Lists.newArrayList();
        for (Object tmp : source) {
            T destinationObject = dozer.map(tmp, destinationClass);
            destinationList.add(destinationObject);
        }
        return destinationList;
    }

    /**
     * 基于Dozer将对象A的值拷贝到对象B中.
     */
    public static void copy(Object source, Object destination) {
        dozer.map(source, destination);
    }

    /**
     * 基于Dozer转换对象的类型.
     *
     * @param <U>
     */
    public static <T, U> T transfer(U source, Class<T> destinationClass, MyTransfer<U, T> myTransfer) {
        if (source == null) {
            return null;
        }
        T to = dozer.map(source, destinationClass);
        myTransfer.transfer(source, to);
        return to;
    }

    /**
     * 此方法只支持可变类型
     *
     * @param fromList
     * @param toClazz
     * @return
     */
    public static <U, V> List<V> transferList(List<U> fromList, Class<V> toClazz, MyTransfer<U, V> myTransfer) {
        if (CollectionUtils.isEmpty(fromList)) {
            return null;
        }
        List<V> result = new ArrayList<V>();
        for (U u : fromList) {
            V v = (V) DozerUtils.transfer(u, toClazz);
            myTransfer.transfer(u, v);
            result.add(v);
        }
        return result;
    }

}
